# coding:utf-8

import os
import set_env_path     # 请勿删除


from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker


Base = declarative_base()
engine = create_engine(
    os.getenv('DATABASE_URL')
    or 'mysql+pymysql://root:@127.0.0.1/wph_www_prod?charset=utf8'
)
Session = sessionmaker(bind=engine)
session = Session()


def update_had_paid():
    """
    create by ld_wu 2017-10-30
    更新用户是否已付费的记录
    """
    select_paid_sql = """
    select distinct uid as user_id from bill
    where type = 1 and sub_type = 2 and status = 200
    and deleted_at = 0
    """

    update_paid_sql = """
    update user set has_paid=TRUE where id=:user_id
    """

    for each in session.execute(select_paid_sql).fetchall():
        print(each.user_id)
        session.execute(
            update_paid_sql,
            {
                'user_id': each.user_id
            }
        )
        session.commit()


if __name__ == '__main__':
    update_had_paid()
